package IO;

import java.io.*;
import java.util.Scanner;

//拷贝一个文件
public class Demo12 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要拷贝的源路径");
        String src = scanner.next();
        System.out.println("请输入要拷贝的目标路径");
        String dest = scanner.next();
        File srcFile = new File(src);
        if(!srcFile.isFile()) {   //判断是否是普通文件
            System.out.println("输入的源路径不对");
            return;
        }
        try(InputStream inputStream = new FileInputStream(srcFile)) {
            try(OutputStream outputStream = new FileOutputStream(dest)) {
                byte[] bytes = new byte[1024];
                while (true) {
                    int len = inputStream.read(bytes);
                    if(len == -1) {
                        break;
                    }
                    outputStream.write(bytes,0,len);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
